package com.anuprayog.convertunits;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class TimeActivity extends Activity {

	private EditText text, result;
	private Spinner spin1, spin2;
	private TextView resultString, resultString1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_time);
		
		text = (EditText) findViewById(R.id.editText1);
		spin1 = (Spinner) findViewById(R.id.spinner1);
		spin2 = (Spinner) findViewById(R.id.spinner2);
		result = (EditText) findViewById(R.id.editText2);
		resultString = (TextView) findViewById(R.id.textView4);
		resultString1 = (TextView) findViewById(R.id.textView5);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.time, menu);
		return true;
	}

	/*@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}*/

	//final Button button = (Button) findViewById(R.id.button1);
    // button.setOnClickListener(new View.OnClickListener() {
    	
	public void onClick(View view) {
			if (text.getText().length() == 0) {
				Toast.makeText(this, "Please enter a valid number",
						Toast.LENGTH_LONG).show();
				return;
			}

			double input = Double.parseDouble(text.getText().toString());
			String unit1 = spin1.getSelectedItem().toString();
			String unit2 = spin2.getSelectedItem().toString();
			double secondvalue;
			double d;
			
			if (unit1.equals("Microsecond"))
			{
				secondvalue = input*(0.000001);
			}
			
			else if (unit1.equals("Millisecond"))
			{
				secondvalue = input*(0.001);
			}
			
			else if (unit1.equals("Second"))
			{
				secondvalue = input;
			}
			
			else if (unit1.equals("Minute"))
			{
				secondvalue = input*(60);
			}
			
			else if (unit1.equals("Hour"))
			{
				secondvalue = input*(3600);
			}
			
			else if (unit1.equals("Day"))
			{
				secondvalue = input*(86400);
			}
			
			else if (unit1.equals("Week"))
			{
				secondvalue = input*(604800);
			}
			
			else if (unit1.equals("Fortnight"))
			{
				secondvalue = input*(1209600);
			}
			
			else if (unit1.equals("Microfortnight"))
			{
				secondvalue = input*(1.209600);
			}
			
			else if (unit1.equals("Gregorian Year"))
			{
				secondvalue = input*(31556952);
			}
			
			else if (unit1.equals("Julian Year"))
			{
				secondvalue = input*(31557600);
			}
			
			else if (unit1.equals("Prana"))
			{
				secondvalue = input*(4);
			}
			
			else if (unit1.equals("Vighati"))
			{
				secondvalue = input*(24);
			}
			
			else if (unit1.equals("Ghatika"))
			{
				secondvalue = input*(1440);
			}
			
			else if (unit1.equals("Muhurta"))
			{
				secondvalue = input*(2880);
			}
			
			else
			{
				secondvalue = 0;
			}
			
			
			if (unit2.equals("Microsecond")){
				d = secondvalue*(1000000);
			}
			
			else if (unit2.equals("Millisecond")){
				d = secondvalue*(1000);
			}
			
			else if (unit2.equals("Second")){
				d = secondvalue;
			}
			
			else if (unit2.equals("Minute")){
				d = secondvalue*(1/60d);
			}
			
			else if (unit2.equals("Hour")){
				d = secondvalue*(1/3600d);
			}
			
			else if (unit2.equals("Day")){
				d = secondvalue*(1/86400d);
			}
			
			else if (unit2.equals("Week")){
				d = secondvalue*(1/604800d);
			}
			
			else if (unit2.equals("Fortnight")){
				d = secondvalue*(1/1209600d);
			}
			
			else if (unit2.equals("Microfortnight")){
				d = secondvalue*(1/1.209600);
			}
			
			else if (unit2.equals("Gregorian Year")){
				d = secondvalue*(1/31556952d);
			}
			
			else if (unit2.equals("Julian Year")){
				d = secondvalue*(1/31557600d);
			}
			
			else if (unit2.equals("Prana")){
				d = secondvalue*(0.25);
			}
			
			else if (unit2.equals("Vighati")){
				d = secondvalue*(1/24d);
			}
			
			else if (unit2.equals("Ghatika")){
				d = secondvalue*(1/1440d);
			}
			
			else if (unit2.equals("Muhurta")){
				d = secondvalue*(1/2880d);
			}
			
			else
			{
				d = 0;
			}
		
			result.setText(String.valueOf(d));
			
			resultString.setText(String.valueOf(input) + " " + unit1 + " =");
			resultString1.setText(String.valueOf(d) + " " + unit2);
			
		}
	
	
	public void goBack(View view) {
		finish();
	}
		
	public void goSwap(View view) {
		int spin1Index = spin1.getSelectedItemPosition();

		spin1.setSelection(spin2.getSelectedItemPosition());
		spin2.setSelection(spin1Index);
	}
}
